package net.bolue.console.data.model;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.bolue.console.data.UpdateInfo;
import net.bolue.console.model.BusiBase;
import net.bolue.sysmng.db.DbOpUtil;
import net.bolue.sysmng.exception.BolueException;
import net.bolue.sysmng.model.ColInfoModel;
import net.bolue.sysmng.model.ReportResModel;
import net.bolue.sysmng.model.UpdResModel;
import net.bolue.sysmng.tblbean.ColInfo;
import net.bolue.sysmng.tblbean.DbopResMap;
import net.bolue.sysmng.tblbean.UpdRes;
import net.bolue.sysmng.tblbean.UpdTabPage;
import net.bolue.sysmng.util.DictUtil;
import net.bolue.web.entry.FormDataOption;
import net.bolue.web.entry.FormElemTag;
import net.bolue.web.report.RepConstants;
import net.bolue.web.util.RequestUtil;
import net.bolue.web.util.TagUtil;

/**
 * 更新信息业务
 * @author Administrator
 *
 */
public class UpdateInfoModel extends BusiBase{

	private UpdateInfo updateInfo;
	private UpdResModel updResModel;
	private ColInfoModel colInfoModel;
	
	public UpdateInfoModel(){
		super();
	}
	
	public UpdateInfoModel(HttpServletRequest request)throws BolueException{
		super(request);
		this.updateInfo = new UpdateInfo();
		init();
	}
	
	public UpdateInfoModel(HttpServletRequest request,UpdRes updRes)throws BolueException{
		super(request);
		this.updateInfo = new UpdateInfo();
		this.updateInfo.setUpdRes(updRes);
		init();
	}
	
	public void init() throws BolueException{
		this.initParam();
		this.updResModel = new UpdResModel();
		this.colInfoModel = new ColInfoModel();
	}
	
	/**
	 * 获得更新信息
	 * @return
	 * @throws BolueException
	 */
	public UpdateInfo getUpdInfo() throws BolueException{
		String subSys = this.updateInfo.getSubSys();
		String updateId = this.updateInfo.getUpdateId();
		UpdRes updRes = this.updateInfo.getUpdRes();
		if(updRes==null){
			updRes = updResModel.getUpdRes(subSys, updateId);
			updateInfo.setUpdRes(updRes);
		}
		if(updateInfo.getTabPage()==null){
			List<UpdTabPage> tabPageList = updResModel.getTabPage(subSys, updateId);
			updateInfo.setTabPage(tabPageList);
		}
		String tableName = updRes.getTableName();
		List<ColInfo> colInfoList = updateInfo.getColInfoList();
		if(colInfoList==null){
			colInfoList = colInfoModel.getColInfoList(subSys, tableName);
			updateInfo.setColInfoList(colInfoList);
		}
		
		this.parseColList();
		if(updRes.getUpdType()==1){
			putData();
		}
		return updateInfo;
	}
	
	/**
	 * 初始化更新参数
	 * @throws BolueException
	 */
	public void initParam() throws BolueException{
		if(this.updateInfo==null){
			return;
		}
		UpdRes updRes = this.updateInfo.getUpdRes();
		if(updRes!=null){
			this.updateInfo.setSubSys(updRes.getSubSys());
			this.updateInfo.setUpdateId(updRes.getUpdateId());
			return;
		}
		String subSys = RequestUtil.getParamValue(request, RepConstants.REP_SUBSYS_KEY);
		if (subSys == null){
			throw new BolueException(1, "必须提供request参数subSys!");
		}
		String updateId = RequestUtil.getParamValue(request, RepConstants.REP_UPDATEID_KEY);
		if (updateId == null){
			String repId = RequestUtil.getParamValue(request, RepConstants.REP_REPID_KEY);
			if (repId == null){
				throw new BolueException(1, "必须提供request参数updateID!");
			}
			int i = new Integer(repId).intValue();
			ReportResModel reportResModel = new ReportResModel();
			DbopResMap dbopResMap = reportResModel.getDBOpResMap(subSys, i, 4);
			if (dbopResMap == null){
				throw new BolueException(1, "没有找到报表" + subSys + "." + i + "的更新资源映谢!");
			}
			updateId = dbopResMap.getResId();
		}
		this.updateInfo.setSubSys(subSys);
		this.updateInfo.setUpdateId(updateId);
	}
	
	/**
	 * 处理表列信息
	 * @throws BolueException
	 */
	public void parseColList() throws BolueException{
		if(this.updateInfo==null){
			return;
		}
		List<ColInfo> colInfoList = this.updateInfo.getColInfoList();
		if(colInfoList==null||colInfoList.size()<1){
			return;
		}
		List<FormElemTag> elemTagList = new ArrayList<FormElemTag>();
		String[] subSysArray = new String[colInfoList.size()];
		Integer[] dicKindArray = new Integer[colInfoList.size()];
		Integer[] dicGrpIdArray = new Integer[colInfoList.size()];
		int k = 0;
		UpdRes updRes = this.updateInfo.getUpdRes();
		if(updRes==null){
			return;
		}
		String dispCols = updRes.getDispCols();
		if(dispCols!=null&&dispCols.length()>0){
			dispCols = ","+dispCols+",";
		}else{
			dispCols = "";
		}
		for(ColInfo colInfo : colInfoList){
			String colName = colInfo.getColName();
			FormElemTag formElemTag = TagUtil.colToField(colInfo);
			Object initValue = request.getParameter("col_"+colName);
			formElemTag.setInitValue(initValue);
			if(colInfo.getTagType()!=4){
				if(dispCols.matches(".*,"+colName+",.*")){
					formElemTag.setTagType(-1);
				}
			}
			elemTagList.add(formElemTag);
			subSysArray[k] = colInfo.getSubSys();
			dicKindArray[k] = colInfo.getDicKind();
			dicGrpIdArray[k] = colInfo.getDicGrpid();
			k++;
		}
		
		Map<String,List<FormDataOption>> dicsMap = DictUtil.getDicMaps(subSysArray, dicKindArray, dicGrpIdArray);
		this.updateInfo.setDicsMap(dicsMap);
		this.updateInfo.setElemTagList(elemTagList);
		
	}
	
	/**
	 * 获得要更新数据
	 * @return
	 * @throws BolueException
	 */
	public void putData() throws BolueException{
		if(this.updateInfo==null){
			return;
		}
		UpdRes updRes = this.updateInfo.getUpdRes();
		if(updRes==null){
			return;
		}
		//获得数据表
		String resTable = updRes.getTableName();
		if("".equals(resTable)){
			return;
		}
		List<ColInfo> colInfoList = updateInfo.getColInfoList();
		if(colInfoList==null||colInfoList.size()<1){
			return;
		}
		Map<String,Object> whereMap = new HashMap<String,Object>();
		for(ColInfo colInfo : colInfoList){
			String keyName = colInfo.getColName();
			String keyValue = RequestUtil.getParamValue(request, "col_"+keyName);
			if(colInfo.getIsPkey()==1&&(keyValue==null||"".equals(keyValue))){
				throw new BolueException(1, "在request中，必须提供参数"+"col_"+keyName+"!");
			}
			if(colInfo.getIsPkey()==1||(keyValue!=null&&!"".equals(keyValue))){
				Object parseValue = RequestUtil.parseValue(colInfo.getJType(), keyValue);
				whereMap.put(keyName, parseValue);
			}
		}
		
		DbOpUtil dbOpUtil = new DbOpUtil(this.updateInfo.getSubSys());
		Map<String,Object> dataMap = dbOpUtil.getOne(resTable, null, whereMap);
		this.updateInfo.setDataMap(dataMap);
	}
	
}
